import { FormInstance } from 'antd';
import { getPosIDs, getUserID } from '@/utils';
import { BZZ_CHECKSTATE, SHTH_CHECKSTATE } from '../../Acommon';
import { SHZ_SQSTATE, QC_SQSTATE, SQ_flowBusinessType } from '../common/Format';

const AllBtnsConfig = (
  curRes: Record<string, any>,
  detailInfo: Record<string, any>,
  nextBtnShow: boolean,
  applyForm: FormInstance,
  oldApplyInfo: Record<string, any>,
  pageType: string,
) => {
  let isShowBtn = {
    allBtnDiv: false,
    saveBtn: false, //暂存按钮

    revokeFlagBtn: false, //撤销按钮
    invalBoolBtn: false, //暂停开始按钮
    stopProcInstBtn: false, //流程作废接口
    applyBtnDiv: false, //提交按钮
    nextBtnDiv: false, //下一步按钮
    editBtnDiv: false, //编辑按钮

    isShowCheckList: false, //是否显示流程记录
    revokeBtnDiv: false, //撤销按钮
    cancelBtnDiv: false, //作废

    flowHandList: {}, //流程办理数据
  };

  /** 暂存按钮 */
  if (detailInfo && detailInfo?.id) {
    if (
      detailInfo?.state &&
      curRes?.['/purchaseApplyController/edit'] &&
      ((detailInfo?.state == QC_SQSTATE() && !detailInfo?.checkState) ||
        (detailInfo?.state == QC_SQSTATE() && detailInfo?.checkState == BZZ_CHECKSTATE()) ||
        (detailInfo?.state == QC_SQSTATE() &&
          detailInfo?.checkState == SHTH_CHECKSTATE() &&
          detailInfo?.paramObject &&
          detailInfo?.applyerID == getUserID() &&
          detailInfo?.applyPositionID == getPosIDs()))
    ) {
      isShowBtn['saveBtn'] = true;
      isShowBtn['allBtnDiv'] = true;
    }
  } else {
    if (curRes?.['/purchaseApplyController/add']) {
      if ((detailInfo?.state && detailInfo?.state == QC_SQSTATE() && !detailInfo?.checkState) || typeof detailInfo?.state == 'undefined') {
        isShowBtn['saveBtn'] = true;
        isShowBtn['allBtnDiv'] = true;
      }
    }
  }

  /** 流程按钮 */
  if (detailInfo?.state == SHZ_SQSTATE()) {
    isShowBtn['revokeFlagBtn'] = true;
    isShowBtn['stopProcInstBtn'] = true;
  }

  // 判断提交按钮
  if (curRes?.['/purchaseApplyController/startProcess/{starterDeptID}'] && detailInfo?.id && detailInfo?.state == QC_SQSTATE()) {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['applyBtnDiv'] = true;
  }

  // 下一步按钮
  if (nextBtnShow && pageType != 'detail') {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['nextBtnDiv'] = true;
  }

  // 编辑按钮
  if (curRes?.['/purchaseApplyController/edit'] && detailInfo?.id && detailInfo?.state == QC_SQSTATE() && pageType == 'detail') {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['editBtnDiv'] = true;
  }

  // 作废
  if (
    curRes['/purchaseApplyController/cancel'] &&
    detailInfo?.id &&
    detailInfo?.ownerID == getUserID() &&
    detailInfo?.state == QC_SQSTATE() &&
    !detailInfo?.checkState
  ) {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['cancelBtnDiv'] = true;
  }

  // 撤销
  if (
    curRes['/purchaseApplyController/invalid'] &&
    // detailInfo?.id &&
    // detailInfo?.ownerID == getUserID() &&
    // detailInfo?.state == SHZ_SQSTATE()

    detailInfo?.id &&
    detailInfo?.applyerID == getUserID() &&
    detailInfo?.applyPositionID == getPosIDs() &&
    (detailInfo?.state == SHZ_SQSTATE() || (detailInfo?.state == QC_SQSTATE() && detailInfo?.checkState == SHTH_CHECKSTATE()))
  ) {
    isShowBtn['allBtnDiv'] = true;
    // isShowBtn['revokeBtnDiv'] = true;
  }

  /** 处理流程办理数据 */
  const taskType: string = SQ_flowBusinessType();
  const taskHandInfo: Record<string, any> = detailInfo?.paramObject?.[taskType];
  if (
    taskHandInfo &&
    taskHandInfo?.mactivitiTaskList?.length > 0 &&
    taskHandInfo?.mactivitiTaskList?.[0]?.assigneePositionID == getPosIDs() &&
    taskHandInfo?.mactivitiTaskList?.[0]?.assigneeID == getUserID()
  ) {
    isShowBtn['invalBoolBtn'] = true;
    isShowBtn['allBtnDiv'] = true;
    // isShowBtn['flowHandList'] = taskHandInfo?.mactivitiTaskList?.[0];
    isShowBtn['flowHandList'] = detailInfo;
  }

  //是否显示流程记录
  if (taskHandInfo && JSON.stringify(taskHandInfo) != '{}') {
    isShowBtn['isShowCheckList'] = true;
  }

  return isShowBtn;
};

export { AllBtnsConfig };
